package org.itboys.store.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.MongoUtils;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.store.entity.ProductStorage;
import org.itboys.store.service.StoreProductStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by huml on 2016/5/17.
 *仓库管理
 */
@RestController
@RequestMapping("/store/storage")
public class StoreProductStorageController extends BaseController {

    @Autowired
    private StoreProductStorageService storeProductStorageService;

    /**
     * 页面跳转
     * @param model
     * @return
     */
    @RequestMapping("/toStorageVM")
    public ModelAndView toStorageVM(Model model){
        return new ModelAndView("ace/store/storage");
    }

    /**
     * 仓库列表
     * @param request
     * @param response
     */
    @RequestMapping("/list")
    public void list(HttpServletRequest request, HttpServletResponse response){
        Page<ProductStorage> page = storeProductStorageService.pageQuery(request);
        JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
    }

    /**
     * 根据id获取单条信息
     * @param id
     * @param response
     */
    @RequestMapping("/getById")
    public void getById(@RequestParam("id")Long id,
                        HttpServletResponse response){
        AjaxUtils.renderJson(response, storeProductStorageService.getById(id));
    }

    /**
     * 编辑仓库信息
     * @param productStorage
     * @param response
     */
    @RequestMapping("/save")
    public void save(@ModelAttribute ProductStorage productStorage,
                     HttpServletResponse response){
        try {
            if(productStorage.getId() == 0){
                storeProductStorageService.save(productStorage);
            } else {
                MongoUtils.fill(productStorage, storeProductStorageService.getById(productStorage.getId()));
                storeProductStorageService.update(productStorage);
            }
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("/store/storage/save error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 删除
     * @param id
     * @param response
     */
    @RequestMapping("/delete")
    public void delete(@RequestParam("id")Long id,
                       HttpServletResponse response){
        try {
            storeProductStorageService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("/product/storage/delete error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

}
